﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LetiLib.Device;

namespace LedCenter.GUI
{
    public partial class DeviceForm : Form
    {
        public bool isChangePassMode { get; set; }
        public BaseDevice DeviceInfo { get; set; }
        public DeviceForm()
        {
            InitializeComponent();
            // Device type combobox initiation
            cbType.DataSource = Enum.GetValues(typeof(DEVICE_TYPE));
            cbType.SelectedItem = DEVICE_TYPE.UNKNOW;

            // Device type combobox initiation
            cbMode.Visible = false;
            cbMode.DataSource = Enum.GetValues(typeof(SMS_MODE));

            isChangePassMode = false;
            showMode(isChangePassMode);
        }

        // edit mode
        public DeviceForm(BaseDevice baseDevice)
        {
            InitializeComponent();
            cbType.DataSource = Enum.GetValues(typeof(DEVICE_TYPE));

            // Device type combobox initiation
            
            cbMode.DataSource = Enum.GetValues(typeof(SMS_MODE));
            cbMode.Visible = false;

            DeviceInfo = baseDevice;

            tbName.Text = baseDevice.Name;
            cbType.SelectedItem = baseDevice.Type;
            tbPass.Text = baseDevice.Password;
            if (DeviceInfo.Type == DEVICE_TYPE.SMS)
            {
                tbData.Text = ((SMSDevice)baseDevice).PhoneNumber;
                tbPort.Visible = false;
                
                cbMode.SelectedItem = ((SMSDevice)baseDevice).Mode;
                cbMode.Visible = true;
                lbData.Text = "Phone number";
                lbExtra.Text = "Mode";
            }
            else if (DeviceInfo.Type == DEVICE_TYPE.TCP)
            {
                tbData.Text = ((TCPDevice)baseDevice).IP;
                tbPort.Text = ((TCPDevice)baseDevice).Port.ToString();
                tbPort.Visible = true;
                cbMode.Visible = false;
                lbData.Text = "IP Address";
                lbExtra.Text = "Port";
            }
            

            isChangePassMode = true;
            showMode(isChangePassMode);
        }

        private void showMode(bool mode)
        {
            if (isChangePassMode)
            {
                lbTitle.Text = "Change Password";
                btnOK.Text = "Change";
                tbName.Enabled = false;
                tbData.Enabled = false;
                tbPort.Enabled = false;
                cbType.Enabled = false;
                cbMode.Enabled = false;
                tbPass.Enabled = true;

                tbName.ReadOnly = true;
                tbData.ReadOnly = true;
                tbPort.ReadOnly = true;
                tbPass.ReadOnly = false;
            }
            else
            {
                lbTitle.Text = "Create new device";
                btnOK.Text = "Create";
                tbName.Enabled = true;
                tbData.Enabled = true;
                tbPort.Enabled = true;
                cbType.Enabled = true;
                cbMode.Enabled = true;
                tbPass.Enabled = true;

                tbName.ReadOnly = false;
                tbData.ReadOnly = false;
                tbPort.ReadOnly = false;
                tbPass.ReadOnly = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //DeviceInfo.d
            // TODO: create Device object TCP/SMS
            // 0 means we need to create TCP device
            if ((DEVICE_TYPE)Enum.Parse(typeof(DEVICE_TYPE), cbType.SelectedItem.ToString()) == DEVICE_TYPE.TCP)
            {
                TCPDevice devTmp = new TCPDevice(tbName.Text, tbIMEI.Text,tbPass.Text, tbData.Text, int.Parse(tbPort.Text));
                // use xml validator to valid object
                DeviceInfo = devTmp;
            }
            else if ((DEVICE_TYPE)Enum.Parse(typeof(DEVICE_TYPE), cbType.SelectedItem.ToString()) == DEVICE_TYPE.SMS)
            {
                SMS_MODE mode = (SMS_MODE)Enum.Parse(typeof(SMS_MODE), cbMode.SelectedItem.ToString());
                SMSDevice devTmp = new SMSDevice(tbName.Text,tbIMEI.Text,tbPass.Text, tbData.Text, mode);
                // use xml validator to valid object
                DeviceInfo = devTmp;
            }
            else if ((DEVICE_TYPE)Enum.Parse(typeof(DEVICE_TYPE), cbType.SelectedItem.ToString()) == DEVICE_TYPE.UNKNOW)
            {
                // use xml validator to valid object
                //DeviceInfo.Name = tbName.Text;
                //DeviceInfo.Password = tbPass.Text;
            }
            if (isValid)
            {
                //btnOK.DialogResult = DialogResult.OK;
                this.DialogResult = DialogResult.OK;
                this.Dispose();
            }
            else
            {
                isValid = true;
                MessageBox.Show("Input data error, please check!");
            }
        }
        bool isValid = false;
        private void DeviceForm_Load(object sender, EventArgs e)
        {
            
            //cbMode.SelectedItem = SMS_MODE.PDU;
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cbType.SelectedItem == "") return;
            if ((DEVICE_TYPE)Enum.Parse(typeof(DEVICE_TYPE), cbType.SelectedItem.ToString()) == DEVICE_TYPE.TCP)
            {
                tbPort.Visible = true;
                cbMode.Visible = false;
                lbData.Text = "IP Address";
                lbExtra.Text = "Port";
            }
            else if ((DEVICE_TYPE)Enum.Parse(typeof(DEVICE_TYPE), cbType.SelectedItem.ToString()) == DEVICE_TYPE.SMS)
            {
                tbPort.Visible = false;
                cbMode.Visible = true;
                lbData.Text = "Phone number";
                lbExtra.Text = "Mode";
            }
            else if ((DEVICE_TYPE)Enum.Parse(typeof(DEVICE_TYPE), cbType.SelectedItem.ToString()) == DEVICE_TYPE.UNKNOW)
            {

            }
        }
    }
}
